package com.yyw.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyw.common.utils.PageUtils;
import com.yyw.common.utils.Query;
import com.yyw.common.utils.R;
import com.yyw.gulimall.ware.dao.WareSkuDao;
import com.yyw.gulimall.ware.dto.SkuInfoStockDto;
import com.yyw.gulimall.ware.entity.WareSkuEntity;
import com.yyw.gulimall.ware.feign.ProductFeign;
import com.yyw.gulimall.ware.service.WareSkuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


@Service("wareSkuService")
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    private final ProductFeign productFeign;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
        String wareId = (String) params.get("wareId");
        String skuId = (String) params.get("skuId");
        if (!StringUtils.isEmpty(wareId) && !Objects.equals("0", wareId)) {
            queryWrapper.lambda().eq(WareSkuEntity::getWareId, wareId);
        }
        if (!StringUtils.isEmpty(skuId) && !Objects.equals("0", skuId)) {
            queryWrapper.lambda().eq(WareSkuEntity::getSkuId, skuId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void putWare(Long skuId, Long wareId, Integer skuNum) {
        // 获取SKU商品信息
        String skuName = "";
        try {
            R info = productFeign.info(skuId);
            if (Objects.equals(info.get("code"), 0)) {
                Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");
                skuName = (String) data.get("skuName");
            }
        } catch (Exception e) {
            log.error("远程调用发生了错误", e);
        }

        // 查询是否存在
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(WareSkuEntity::getSkuId, skuId).eq(WareSkuEntity::getWareId, wareId);
        WareSkuEntity entity = this.getOne(wrapper);
        if (Objects.equals(null, entity)) {
            // 新增操作
            this.save(new WareSkuEntity(null, skuId, wareId, skuNum, skuName, 0));
        } else {
            entity.setStock(entity.getStock() + skuNum);
            this.updateById(entity);
        }
    }

    @Override
    public List<SkuInfoStockDto> getStockByBulk(List<Long> skuIds) {
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(WareSkuEntity::getSkuId, skuIds);
        List<WareSkuEntity> list = this.list(queryWrapper);
        List<SkuInfoStockDto> collect = null;
        if (!CollectionUtils.isEmpty(list)) {
            collect = list.stream().map(e -> {
                int count = e.getStock() - e.getStockLocked();
                SkuInfoStockDto dto = SkuInfoStockDto.builder()
                        .skuId(e.getSkuId())
                        .hasStock(count > 0).build();
                return dto;
            }).collect(Collectors.toList());
        }
        return collect;
    }

}